# -*- coding: utf-8 -*-
#*******************************************************************************
#   
#   http://www.mangago.me/read-manga/sekaiichi_hatsukoi/ 解析器
#
#*******************************************************************************
from base import *

tag='mangago'

#---------------------------- 验证模块 ----------------------------

def url_judgement(url):
    if re.match(r'http://www.mangago.me/read-manga/[\w_]+/',url):
        return valid_check(url)
    return False

def valid_check(url):
    print('{} catch {}'.format(tag,url))
    content=request_for(url,common_headers);
    if content:
        return get_loader(url)
    return False

# 下载器获取
def get_loader(url):
    return MangagoLoader(url,MangagoPageParser,MangagoVolParser)

#---------------------------- 下载器 ----------------------------

class MangagoLoader(ComicLoader):
    def __init__(self,url,comic_page_parser,comic_vol_parser):
        super().__init__(url,comic_page_parser,comic_vol_parser)

#---------------------------- 解析器 ----------------------------

class MangagoPageParser(ComicPageParser):
    def __init__(self,comic):
        super().__init__(comic)
    def work(self,content):
        # title
        self.comic.title=re.compile(r'<h1>(.*?)</h1>',re.S|re.M).findall(content)[0].strip()
        # description
        self.comic.description=re.compile(r'<div class="manga_summary">(.*?)<br',re.S|re.M).findall(content)[0]
        # cover
        self.comic.cover=re.compile(r'<meta property="og:image" content="(.*?)\?',re.S|re.M).findall(content)[0]
        # vols
        vols_content=re.compile(r'<table class="listing" id="chapter_table">(.*?)</table>',re.S|re.M).findall(content)[0]
        vols_content=re.compile(r'<h4>(.*?)</h4>',re.S|re.M).findall(vols_content)
        for vol in vols_content:
            url=re.compile(r'href="(.*?)"',re.S|re.M).findall(vol)[0]
            title=re.compile(r'<b>(.*?)</b>',re.S|re.M).findall(vol)[0]
            self.comic.vols.append(InfoVol(url,title))

class MangagoVolParser(ComicVolParser):
    def __init__(self):
        super().__init__()
    def work(self,content):
        urls=re.compile(r'var imgsrcs = new Array\((.*?)\);',re.S|re.M).findall(content)[0]
        urls=urls.replace("'",'').split(',')
        for url in urls:
            self.pic_urls.append((url,zero_prefix(self.pic_index)))
            self.pic_index+=1